package org.flint92.springcloud.ribbon.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author flint92
 */
@RestController
public class ClientController {

    private final RestTemplate restTemplate;

    private final String serviceProviderName;

    @Autowired
    public ClientController(RestTemplate restTemplate,
                            @Value("${service.provider.name}") String serviceProviderName) {
        this.restTemplate = restTemplate;
        this.serviceProviderName = serviceProviderName;
    }

    @GetMapping("/greeting")
    public String greeting(@RequestParam String message) {
//        return this.restTemplate.getForObject("http://127.0.0.1:6001/greeting?message=" + message,
//                String.class);
        String url = "http://" + this.serviceProviderName + "/greeting?message=" + message;
        return this.restTemplate.getForObject(url, String.class);
    }
}
